(function (aopc) {
    aopc.addExpandPage(853, function (get) {
        const $ = get('jquery', '$');
        const Bridge = get('UtilsCommon', 'Bridge');

        return {
            expandInit() {
                this.filesProperty = this.getProperty('Files');
                this.filesProperty.dataSourceChange = this.filesDataSourceChange.bind(this);
                this.upload_file_view = this.getProperty('upload_file_view');
                this.file_count = this.getProperty('file_count');
                this.ok_button = this.getProperty('ok_button');
                this.buttons_view = this.getProperty('buttons_view');
                this.upload_status_view = this.getProperty('upload_status_view');

                Bridge.setReceiveFunction('uploadFiles', (data) => this.setFiles(data.Files));
                Bridge.setReceiveFunction('uploadProgress', (data) => this.setUploadProgress(data));
            },
            setFiles(dataList) {
                if (dataList && dataList.length > 0) {
                    let list = (this.filesProperty.dataSource || []).map(m => m);
                    let blHas = false;
                    dataList.forEach(d => {
                        const item = list.find(f => f.FilePath === d.FilePath);
                        if (!item) {
                            list.push(d);
                            blHas = true;
                        }
                    })
                    if (blHas) this.filesProperty.setValue(list);
                    else this.alert('选择的文件都已存在！');
                }
            },
            show(property, data) {
                this.uploadButtonProperty = property;
                this.isImportTemplate = data && data.Files && data.Files.length > 0;
                if (this.isImportTemplate) {
                    this.filesProperty.dataSource = data.Files
                    this.buttons_view.isVisible = false;
                    this.ok_button.isReUpload = false;
                    this.upload_status_view.isVisible = true;
                }
                else {
                    this.filesProperty.dataSource = [];
                    this.buttons_view.isVisible = true;
                    this.ok_button.isReUpload = false;
                    this.upload_status_view.isVisible = false;
                }
                this.upload_file_view.setIsVisible(true);
            },
            startUpload({ property }) {
                if (property.isReUpload) {
                    this.buttons_view.setIsVisible(true);
                    this.upload_status_view.setIsVisible(false);
                    this.filesProperty.setValue([]);
                    property.setValue('开始上传');
                    property.setIsVisible(false);
                    property.isReUpload = false;
                    return;
                }
                const { dataSource } = this.filesProperty;
                if (!dataSource || dataSource.length === 0) {
                    this.alert('请选择要上传的文件！');
                    return;
                }

                if (dataSource.length > 50) {
                    this.alert('最多只能选择50个文件！');
                    return;
                }

                for (let i = 0; i < dataSource.length; i++) {
                    const d = dataSource[i];
                    if (d.FileSize === 0) {
                        this.alert('文件：' + d.FilePath + '为空文件！');
                        return;
                    }
                }

                property.setIsVisible(false);
                this.buttons_view.setIsVisible(false);
                this.upload_status_view.setIsVisible(true);

                for (let i = 0; i < dataSource.length; i++) {
                    const d = dataSource[i];
                    const tr = document.getElementById('tr_' + d.FileId);
                    tr.childNodes[3].childNodes[0].innerText = '';
                    $(tr).append(`<div id='div_${d.FileId}'></div>`);
                };

                const { MeetingId } = this.pageData;
                Bridge.jsToNative('startUpload', { Files: dataSource, MeetingId });
            },
            setUploadProgress(data) {
                const { FileId, TotalUploadStatus, UploadPercent, Message, Files } = data;

                if (FileId) {
                    const tr = document.getElementById(`tr_${FileId}`);
                    tr.childNodes[2].childNodes[0].innerText = UploadPercent == 100 ? '上传成功' : Message || `上传中（${UploadPercent}%）`;
                    const x = UploadPercent - 100;
                    $(`#div_${FileId}`).css('transform', `translateX(${x}%)`)
                }
                this.upload_status_view.properties[0].setValue(TotalUploadStatus);

                if (Files) {
                    const pageAxis = this.props.getPageAxis();
                    if (this.isImportTemplate) {
                        if (pageAxis.setImportTemplateFiles) pageAxis.setImportTemplateFiles(Files, this.uploadButtonProperty);
                        this.upload_file_view.setIsVisible(false);
                    }
                    else {
                        if (pageAxis.setFiles) pageAxis.setFiles(Files, this.uploadButtonProperty);
                        this.ok_button.setValue('继续上传');
                        this.ok_button.isReUpload = true;
                        this.ok_button.setIsVisible(true);
                    }
                }
            },
            setRowProperties({ property, rowView, p, rowData }) {
                if (p.name === 'DeleteName') {
                    p.eventActionName = 'deleteFile';
                }
            },
            deleteFile({ property }) {
                if (property.data.FileId) {
                    const list = this.filesProperty.dataSource.filter(f => f.FileId !== property.data.FileId);
                    this.filesProperty.setValue(list);
                }
            },
            filesDataSourceChange(dataSource, property) {
                const has = dataSource && dataSource.length > 0;
                this.file_count.setValue(has ? '共 ' + dataSource.length + ' 个文件' : '');
                this.ok_button.setIsVisible(has);
            },
            closeDialog() {
                this.upload_file_view.setIsVisible(false);
                Bridge.jsToNative('closeUpload');
            },
            uploadFiles() {
                Bridge.jsToNative('uploadFiles');
            },
            uploadDirFiles() {
                Bridge.jsToNative('uploadDirFiles');
            },
            clearFiles() {
                this.filesProperty.setValue([]);
            }
        }
    });
})(window.aopc);